<?php
declare(strict_types=1);

namespace App\Controller\Backend;

use App\Logic\AdminExpertLogic;
use Exception;
use Hyperf\HttpServer\Annotation\AutoController;
use Psr\Http\Message\ResponseInterface;

#[AutoController(prefix: '/expert', server: 'backend')]
class ExpertController extends AbstractController
{
    /**
     * 专家列表
     * @return ResponseInterface
     */
    public function lists(): ResponseInterface
    {
        $input = $this->request->all();
        return $this->apiReturn(200, 'succ', AdminExpertLogic::listExpert($input));
    }

    /**
     * 专家详情
     * @return ResponseInterface
     */
    public function detail(): ResponseInterface
    {
        $userId = (int)$this->request->input('userId', 0);
        if (!$userId) {
            return $this->apiReturn(400, '参数错误');
        }
        return $this->apiReturn(200, 'succ', AdminExpertLogic::detailExpert($userId));
    }

    /**
     * 修改专家信息
     * @return ResponseInterface
     */
    public function edit(): ResponseInterface
    {
        $userId = (int)$this->request->input('userId', 0);
        if (!$userId) {
            return $this->apiReturn(400, '参数错误');
        }
        $input = $this->request->all();
        if (empty($input['expertDesc'])) {
            return $this->apiReturn(400, '专家称号不能为空');
        }
        try {
            AdminExpertLogic::editExpert($userId, $input);
            return $this->apiReturn(200, '修改成功');
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }


    /**
     * 方案列表
     * @return ResponseInterface
     */
    public function projectList(): ResponseInterface
    {
        $input = $this->request->all();
        return $this->apiReturn(200, 'succ', AdminExpertLogic::listProject($input));
    }

    /**
     * 专家入驻申请列表
     * @return ResponseInterface
     */
    public function settledList(): ResponseInterface
    {
        $input = $this->request->all();
        return $this->apiReturn(200, 'succ', AdminExpertLogic::listSettled($input));
    }

    /**
     * 处理入驻申请
     * @return ResponseInterface
     */
    public function auditSettled(): ResponseInterface
    {
        $id = (int)$this->request->input('id', 0);
        if (!$id) {
            return $this->apiReturn(400, '参数错误');
        }
        $input = $this->request->all();
        if (empty($input['expertDesc'])) {
            return $this->apiReturn(400, '专家称号不能为空');
        }
        if (!in_array((int)$input['status'], [1, 2])) {
            return $this->apiReturn(400, '处理状态异常');
        }
        try {
            AdminExpertLogic::auditSettled($id, $input);
            return $this->apiReturn(200, '操作成功');
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }
}